#!/bin/sh

fwid=$(bspc query -N -n focused)

wattr wh $fwid | {
	read width height
	if [ $width -gt $height ] ; then
		bspc node -p \~east
	else
		bspc node -p \~south
	fi
}

_bspc_query() {
	bspc query -N -n "$@"
}

recept="$(_bspc_query 'any.leaf.!window')"
presel="$(_bspc_query 'newest.!automatic')"

if [ -n "$recept" ]; then
	target="$recept"
	attention="off"
elif [ -n "$presel" ]; then
	target="$presel"
fi

echo "node=${target:-focused}"
echo "follow=${attention:-on}"

window_id="$1"
window_class="$2"
window_instance="$3"
window_title="$(xwininfo -id "$window_id" | sed ' /^xwininfo/!d ; s,.*"\(.*\)".*,\1,')"

case "$window_class" in
	[Mm]pv|[Gg]picview|[Nn]itrogen|[Aa]randr|[Gg]alculator|[Ff]ont-manager|[Oo]blogout|[Pp]eek|[Ss]kype|[Xx]fce4-appfinder|[Gg]pick|[Gg]mrun|[Xx][Cc]alc|[Pp]avucontrol|[Vv]lc|[Ee]o[mg]|[Ff]eh|[Rr]istretto|[Ss]xiv|[Pp]qiv|[Aa]tril|[Ee]vince|[Zz]athura|scratchpad)
		echo "state=floating"
		echo "center=on"
		;;
	* )
		case "$(xprop -id "$window_id" _NET_WM_WINDOW_TYPE)" in
			*_NET_WM_WINDOW_TYPE_DIALOG*)
				echo "state=floating"
				;;
			*)
				echo "state=tiled"
				;;
		esac
		;;
esac

case "$window_title" in
	'File Operations'*)
		echo "state=floating"
		echo "center=on"
		;;
	scratchpad)
		echo "state=floating"
		;;
esac
